package com.zzx.common;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

/**
 * 
 * @ClassName: DataSourceConfig
 * @Description: 数据源配置
 * @author zzx
 * @date 2022年2月7日 上午11:55:31
 *
 */
@Configuration
public class DataSourceConfig {
	
//	@Value("${spring.datasource.type}")
//	private String jdbcType ;
//	
//	@Value("${spring.datasource.db1.url}")
//	private String mysqlJdbcUrl ;	
//	@Value("${spring.datasource.db1.username}")
//	private String mysqlUsername ;	
//	@Value("${spring.datasource.db1.password}")
//	private String mysqlPassword ;
//	@Value("${spring.datasource.db1.driverClassName}")
//	private String mysqlDriverClassName ;
//	
//	@Value("${spring.datasource.db2.url}")
//	private String pgJdbcUrl ;
//	@Value("${spring.datasource.db2.username}")
//	private String pgUsername ;
//	@Value("${spring.datasource.db2.password}")
//	private String pgPassword ;
//	@Value("${spring.datasource.db2.driverClassName}")
//	private String pgDriverClassName ;
	
	/**
	 * 
	 * @Title: masterDataSource
	 * @Description: 主库配置
	 * @param @param dataSourceProperties
	 * @param @return    参数
	 * @return DataSource    返回类型
	 * @throws
	 * @author：zzx 
	 * @date：2022年2月7日 下午1:36:41
	 */
	//@Primary
	//@ConfigurationProperties("spring.datasource.db1")
	@Bean//(name = "masterDataSource")
    public DataSource masterDataSource() throws Exception {
		HikariDataSource hd = new HikariDataSource();
		hd.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/hnyx?characterEncoding=UTF-8&useUnicode=true&useSSL=true&serverTimezone=GMT%2B8");
        hd.setUsername("root");
        hd.setPassword("1qaz1qaz");
        hd.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return hd;
    }
	/**
     * 从库
     */
	//@ConditionalOnProperty( prefix = "spring.datasource.db2", name = "enable", havingValue = "true")//是否开启数据源开关---若不开启 默认适用默认数据源
	//@ConfigurationProperties("spring.datasource.db2")
    @Bean//(name = "slaveDataSource")
    public DataSource slaveDataSource()  throws Exception{
		HikariDataSource hd = new HikariDataSource();		
		hd.setJdbcUrl("jdbc:postgresql://127.0.0.1:5432/mygis");
        hd.setUsername("postgres");
        hd.setPassword("1qaz1qaz");
        hd.setDriverClassName("org.postgresql.Driver");
    	return hd;
    }

    /**
     * 设置数据源
     */
    @Bean
    @Primary
    public DynamicDataSource dynamicDataSource(@Qualifier("masterDataSource")DataSource masterDataSource, @Qualifier("slaveDataSource")DataSource slaveDataSource) {
      
    	Map<Object, Object> targetDataSources = new HashMap<>();
        DynamicDataSource dynamicDataSource = DynamicDataSource.build();
        targetDataSources.put(DataSourcesType.DB1.name(), masterDataSource);
        targetDataSources.put(DataSourcesType.DB2.name(), slaveDataSource);
        //默认数据源配置 DefaultTargetDataSource
        dynamicDataSource.setDefaultTargetDataSource(masterDataSource);
        //额外数据源配置 TargetDataSources
        dynamicDataSource.setTargetDataSources(targetDataSources);
        //dataSource.setDefaultTargetDataSource(mainDataSource);/
        //dynamicDataSource.afterPropertiesSet();
        return dynamicDataSource;
    }

    @Bean(name = "SqlSessionFactory")
	public SqlSessionFactory test1SqlSessionFactory(@Qualifier("masterDataSource")DataSource masterDataSource, @Qualifier("slaveDataSource")DataSource slaveDataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();		
		bean.setDataSource(this.dynamicDataSource(masterDataSource , slaveDataSource ));
		bean.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath*:com/yx/mapper/*.xml"));//mapping/*.xml"));
		return bean.getObject();
	}
    
    @Bean
    public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) throws Exception {
        return new DataSourceTransactionManager(dataSource);
    }


}
